package com.example.flinktest.sink;

import com.example.flinktest.source.Event;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.util.concurrent.TimeUnit;

public class SinkToFileTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);

        DataStreamSource<Event> stream = env.fromElements(
                new Event("Mary", "./home", 200L),
                new Event("Mary1", "./home1", 400L),
                new Event("Mary2", "./home2", 300L),
                new Event("Mary3", "./home3", 100L),
                new Event("Mary4", "./home4", 600L),
                new Event("Mary5", "./home5", 500L)
        );

        StreamingFileSink<String> fileSink = StreamingFileSink.<String>forRowFormat(
                new Path("./output"),
                new SimpleStringEncoder<>("UTF-8")
        ).withRollingPolicy(DefaultRollingPolicy.builder() //开启新文件策略，避免单个文件太大
                .withRolloverInterval(TimeUnit.MINUTES.toMillis(15))
                .withInactivityInterval(TimeUnit.MINUTES.toMillis(5))
                .withMaxPartSize(1024 * 1024 * 1024)
                .build()).build();

        //将event转换成string写入文件
        stream.map(Event::toString).addSink(fileSink);
        env.execute();
    }
}
